package view;

import model.Ingredient;
import model.IngredientDAO;
import model.Store;
import model.StoreDAO;
import model.requirement3.SpecialPizza;
import model.requirement3.SpecialPizzaDAO;
import model.requirement7.StatisticsDAO;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminView extends JFrame {
    // 主界面按钮
    private JButton storeManagementButton, ingredientManagementButton, pizzaManagementButton;

    // 子功能界面的内容面板
    private JPanel mainPanel;

        public AdminView() {
        setTitle("Pizza Ordering System - Admin");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 主按钮区
        JPanel buttonPanel = new JPanel();
        storeManagementButton = new JButton("店铺管理");
        ingredientManagementButton = new JButton("原材料管理");
        pizzaManagementButton = new JButton("披萨管理");
        JButton statisticsButton = new JButton("销售统计");
        JButton centralMenuButton = new JButton("中央菜单管理"); // 添加中央菜单管理按钮
        buttonPanel.add(storeManagementButton);
        buttonPanel.add(ingredientManagementButton);
        buttonPanel.add(pizzaManagementButton);
        buttonPanel.add(statisticsButton);
        buttonPanel.add(centralMenuButton); // 添加中央菜单管理按钮到面板
        // 内容面板
        mainPanel = new JPanel(new CardLayout());

        // 添加子功能界面
        mainPanel.add(createStoreManagementPanel(), "storeManagement");
        mainPanel.add(createIngredientManagementPanel(), "ingredientManagement");
        mainPanel.add(createPizzaManagementPanel(), "pizzaManagement");
        mainPanel.add(createSalesStatisticsPanel(), "salesStatistics");
        mainPanel.add(createCentralMenuPanel(), "centralMenu"); // 添加中央菜单管理界面
        // 设置布局
        add(buttonPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);

        // 按钮监听器
        storeManagementButton.addActionListener(e -> switchPanel("storeManagement"));
        ingredientManagementButton.addActionListener(e -> switchPanel("ingredientManagement"));
        pizzaManagementButton.addActionListener(e -> switchPanel("pizzaManagement"));
        statisticsButton.addActionListener(e -> switchPanel("salesStatistics"));
        centralMenuButton.addActionListener(e -> switchPanel("centralMenu")); // 添加中央菜单管理按钮监听器
    }

    // 创建中央菜单管理界面
    private JPanel createCentralMenuPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 在这里添加中央菜单管理界面的组件和逻辑
        new CentralMenuView().setVisible(true);
        return panel;
    }



    private void switchPanel(String panelName) {
        CardLayout layout = (CardLayout) mainPanel.getLayout();
        layout.show(mainPanel, panelName);
    }

    // 创建店铺管理界面
    private JPanel createStoreManagementPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JTextField storeIDField = new JTextField(10);
        JTextField storeNameField = new JTextField(10);
        JTextField storeLocationField = new JTextField(10);
        JTextField contactField = new JTextField(10);

        JButton addStoreButton = new JButton("添加店铺");
        JButton viewStoresButton = new JButton("查看店铺");

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("店铺ID:"), gbc);
        gbc.gridx = 1;
        panel.add(storeIDField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("店铺名:"), gbc);
        gbc.gridx = 1;
        panel.add(storeNameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(new JLabel("地点:"), gbc);
        gbc.gridx = 1;
        panel.add(storeLocationField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel("联系方式:"), gbc);
        gbc.gridx = 1;
        panel.add(contactField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        panel.add(addStoreButton, gbc);
        gbc.gridx = 1;
        panel.add(viewStoresButton, gbc);

        // 添加店铺功能
        addStoreButton.addActionListener(e -> {
            try {
                int storeID = Integer.parseInt(storeIDField.getText());
                String name = storeNameField.getText().trim();
                String location = storeLocationField.getText().trim();
                String contact = contactField.getText().trim();

                if (name.isEmpty() || location.isEmpty() || contact.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "所有字段均不能为空！");
                    return;
                }

                StoreDAO storeDAO = new StoreDAO();
                storeDAO.addStore(new Store(storeID, name, location, contact));
                JOptionPane.showMessageDialog(null, "成功添加店铺: " + name);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "店铺ID必须是数字！");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "添加失败: " + ex.getMessage());
            }
        });

        // 查看店铺功能
        viewStoresButton.addActionListener(e -> {
            try {
                StoreDAO storeDAO = new StoreDAO();
                List<Store> stores = storeDAO.getAllStores();

                DefaultTableModel storeTableModel = new DefaultTableModel(new Object[]{"ID", "名称", "地点", "联系方式"}, 0);
                for (Store store : stores) {
                    storeTableModel.addRow(new Object[]{store.getStoreID(), store.getStoreName(), store.getStoreLocation(), store.getContactNumber()});
                }

                JTable storeTable = new JTable(storeTableModel);
                JScrollPane storeScrollPane = new JScrollPane(storeTable);
                storeTable.setFillsViewportHeight(true);

                JFrame storeFrame = new JFrame("店铺信息");
                storeFrame.setSize(600, 400);
                storeFrame.add(storeScrollPane);
                storeFrame.setVisible(true);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "查询失败: " + ex.getMessage());
            }
        });

        return panel;
    }

    // 创建原材料管理界面
    private JPanel createIngredientManagementPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JTextField ingredientIDField = new JTextField(10);
        JTextField ingredientNameField = new JTextField(10);
        JTextField ingredientTypeField = new JTextField(10);
        JTextField ingredientPriceField = new JTextField(10);
        JTextField ingredientStoreIDField = new JTextField(10);

        JButton addIngredientButton = new JButton("添加原材料");
        JButton viewIngredientsButton = new JButton("查看原材料");
        JButton deleteIngredientButton = new JButton("删除原材料");

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("原材料ID:"), gbc);
        gbc.gridx = 1;
        panel.add(ingredientIDField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("原材料名:"), gbc);
        gbc.gridx = 1;
        panel.add(ingredientNameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(new JLabel("类型:"), gbc);
        gbc.gridx = 1;
        panel.add(ingredientTypeField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel("价格:"), gbc);
        gbc.gridx = 1;
        panel.add(ingredientPriceField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        panel.add(new JLabel("店铺ID (关联):"), gbc);
        gbc.gridx = 1;
        panel.add(ingredientStoreIDField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        panel.add(addIngredientButton, gbc);
        gbc.gridx = 1;
        panel.add(viewIngredientsButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 6;
        panel.add(deleteIngredientButton, gbc);

        // 添加原材料功能
        addIngredientButton.addActionListener(e -> {
            try {
                int storeID = Integer.parseInt(ingredientStoreIDField.getText());
                String name = ingredientNameField.getText().trim();
                String type = ingredientTypeField.getText().trim();
                double price = Double.parseDouble(ingredientPriceField.getText());

                if (name.isEmpty() || type.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "原材料名称和类型不能为空！");
                    return;
                }

                IngredientDAO ingredientDAO = new IngredientDAO();
                ingredientDAO.addIngredient(new Ingredient(0, storeID, name, type, price));
                JOptionPane.showMessageDialog(null, "成功添加原材料: " + name);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "店铺ID和价格必须是数字！");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "添加失败: " + ex.getMessage());
            }
        });

        // 查看原材料功能
        viewIngredientsButton.addActionListener(e -> {
            try {
                int storeID = Integer.parseInt(ingredientStoreIDField.getText());

                IngredientDAO ingredientDAO = new IngredientDAO();
                List<Ingredient> ingredients = ingredientDAO.getIngredientsByStoreID(storeID);

                DefaultTableModel ingredientTableModel = new DefaultTableModel(new Object[]{"ID", "名称", "类型", "价格", "店铺ID"}, 0);
                for (Ingredient ingredient : ingredients) {
                    ingredientTableModel.addRow(new Object[]{ingredient.getIngredientID(), ingredient.getName(), ingredient.getType(), ingredient.getPrice(), ingredient.getStoreID()});
                }

                JTable ingredientTable = new JTable(ingredientTableModel);
                JScrollPane ingredientScrollPane = new JScrollPane(ingredientTable);
                ingredientTable.setFillsViewportHeight(true);

                JFrame ingredientFrame = new JFrame("原材料信息");
                ingredientFrame.setSize(600, 400);
                ingredientFrame.add(ingredientScrollPane);
                ingredientFrame.setVisible(true);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "店铺ID必须是数字！");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "查询失败: " + ex.getMessage());
            }
        });

        // 删除原材料功能
        deleteIngredientButton.addActionListener(e -> {
            try {
                int ingredientID = Integer.parseInt(ingredientIDField.getText());

                IngredientDAO ingredientDAO = new IngredientDAO();
                ingredientDAO.deleteIngredient(ingredientID);
                JOptionPane.showMessageDialog(null, "成功删除原材料: ID " + ingredientID);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "原材料ID必须是数字！");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "删除失败: " + ex.getMessage());
            }
        });

        return panel;
    }

    // 创建披萨管理界面
    private JPanel createPizzaManagementPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JTextField storeIDField = new JTextField(10);
        JTextField pizzaNameField = new JTextField(10);
        JTextField toppingField = new JTextField(20);
        JTextField priceField = new JTextField(10);

        JButton addPizzaButton = new JButton("添加特色披萨");
        JButton viewPizzasButton = new JButton("查看所有披萨");
        JButton deletePizzaButton = new JButton("删除特色披萨");

        // 披萨表格
              // 调整表格大小
        DefaultTableModel pizzaTableModel = new DefaultTableModel(new Object[]{"披萨ID", "门店ID", "披萨名称", "配料", "价格"}, 0);
        JTable pizzaTable = new JTable(pizzaTableModel);
        pizzaTable.setPreferredScrollableViewportSize(new Dimension(500, 300));

        // 输入区域
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("店铺ID:"), gbc);
        gbc.gridx = 1;
        panel.add(storeIDField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("披萨名称:"), gbc);
        gbc.gridx = 1;
        panel.add(pizzaNameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(new JLabel("配料:"), gbc);
        gbc.gridx = 1;
        panel.add(toppingField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel("价格:"), gbc);
        gbc.gridx = 1;
        panel.add(priceField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        panel.add(addPizzaButton, gbc);
        gbc.gridx = 1;
        panel.add(viewPizzasButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        panel.add(deletePizzaButton, gbc);

        // 表格显示区域
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        JScrollPane scrollPane = new JScrollPane(pizzaTable);
        panel.add(scrollPane, gbc);

        // DAO 对象
        SpecialPizzaDAO pizzaDAO = new SpecialPizzaDAO();

        // 添加特色披萨功能
        addPizzaButton.addActionListener(e -> {
            try {
                int storeID = Integer.parseInt(storeIDField.getText().trim());
                String name = pizzaNameField.getText().trim();
                String toppings = toppingField.getText().trim();
                double price = Double.parseDouble(priceField.getText().trim());

                if (name.isEmpty() || toppings.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "披萨名称和配料不能为空！");
                    return;
                }

                pizzaDAO.addPizza(storeID, name, toppings, price);
                JOptionPane.showMessageDialog(null, "成功添加特色披萨: " + name);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "店铺ID和价格必须是数字！");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "添加失败: " + ex.getMessage());
            }
        });

        // 查看所有披萨功能
        viewPizzasButton.addActionListener(e -> {
            try {
                int storeID = Integer.parseInt(storeIDField.getText().trim());
                List<SpecialPizza> pizzas = pizzaDAO.getPizzasByStoreID(storeID);

                pizzaTableModel.setRowCount(0); // 清空表格
                for (SpecialPizza pizza : pizzas) {
                    pizzaTableModel.addRow(new Object[]{pizza.getPizzaID(), pizza.getStoreID(), pizza.getName(), pizza.getToppings(), pizza.getPrice()});
                }
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "店铺ID必须是数字！");
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "查询失败: " + ex.getMessage());
            }
        });

        // 删除特色披萨功能
        deletePizzaButton.addActionListener(e -> {
            try {
                int selectedRow = pizzaTable.getSelectedRow();
                if (selectedRow == -1) {
                    JOptionPane.showMessageDialog(null, "请选择要删除的披萨！");
                    return;
                }

                int pizzaID = (int) pizzaTableModel.getValueAt(selectedRow, 0);
                pizzaDAO.deletePizza(pizzaID);
                pizzaTableModel.removeRow(selectedRow);
                JOptionPane.showMessageDialog(null, "成功删除披萨 ID: " + pizzaID);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "删除失败: " + ex.getMessage());
            }
        });

        return panel;
    }

    // 添加销售统计界面
    private JPanel createSalesStatisticsPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 顶部按钮区
        JPanel buttonPanel = new JPanel();
        JButton byDateButton = new JButton("按日期统计");
        JButton byCategoryButton = new JButton("按品类统计");
        JButton byItemButton = new JButton("按单品统计");
        buttonPanel.add(byDateButton);
        buttonPanel.add(byCategoryButton);
        buttonPanel.add(byItemButton);
        panel.add(buttonPanel, BorderLayout.NORTH);

        // 数据表格区
        DefaultTableModel tableModel = new DefaultTableModel();
        JTable statisticsTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(statisticsTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        // DAO 对象
        StatisticsDAO statisticsDAO = new StatisticsDAO();

        // 按日期统计按钮功能
        byDateButton.addActionListener(e -> {
            try {
                List<String[]> results = statisticsDAO.getSalesByDate();
                tableModel.setRowCount(0); // 清空表格
                tableModel.setColumnIdentifiers(new String[]{"日期", "销售额 ($)"});
                for (String[] row : results) {
                    tableModel.addRow(row);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "加载统计数据失败: " + ex.getMessage());
            }
        });

        // 按品类统计按钮功能
        byCategoryButton.addActionListener(e -> {
            try {
                List<String[]> results = statisticsDAO.getSalesByCategory();
                tableModel.setRowCount(0); // 清空表格
                tableModel.setColumnIdentifiers(new String[]{"品类", "销售额 ($)"});
                for (String[] row : results) {
                    tableModel.addRow(row);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "加载统计数据失败: " + ex.getMessage());
            }
        });

        // 按单品统计按钮功能
        byItemButton.addActionListener(e -> {
            try {
                List<String[]> results = statisticsDAO.getSalesByItem();
                tableModel.setRowCount(0); // 清空表格
                tableModel.setColumnIdentifiers(new String[]{"单品名称", "销售量", "销售额 ($)"});
                for (String[] row : results) {
                    tableModel.addRow(row);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "加载统计数据失败: " + ex.getMessage());
            }
        });

        return panel;
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new AdminView().setVisible(true));
    }
}
